// pages/audit/audit.js
var http = require('../../utils/rquest.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    role:true,
    activeName: '',
    active: 0,
    list: [],
    currentobj: {
      0: 1,
      1: 1,
      2: 1
    },
    ptlist:[],
    zjlist:[],
    gjlist:[],
    pttotal: 0,
    zjtotal: 0,
    gjtotal: 0
  },
  //切换tab
  onChange(event) {
    this.setData({
      activeName: event.detail.index,
      active: event.detail.index
    });
    this.getList()
  },
  //获取数据列表
  getList() {
    let type = 0;
    switch (this.data.active) {
      case 0:
        type = 'task_type_1'
        break;
      case 1:
        type = 'task_type_2'
        break;
      case 2:
        type = 'task_type_3'
        break;
    }
    http.orderGetAuditAll({
        orderStatus: 'daishenhe',
        taskType: type,
        creatorId: wx.getStorageSync('userMsg').creator,
        current: this.data.currentobj[this.data.active]
      })
      .then(res => {
        let arr = res.data.msg.list;
        let pttotal = Math.ceil(res.data.msg.total / res.data.msg.pageSize)
        let zjtotal = Math.ceil(res.data.msg.total / res.data.msg.pageSize)
        let gjtotal = Math.ceil(res.data.msg.total / res.data.msg.pageSize)

        if (this.data.active == 0) {
          this.setData({
            pttotal: pttotal
          })
          //////////////////
          if (this.data.currentobj[0] == 1) {
            //如果是当前页我就给你赋值
            this.setData({
              ptlist: arr
            })
          } else {
            if (options == "缓存") {
              this.setData({
                ptlist: this.data.ptlist
              })
            } else {
              //如果不是当前页我就给你拼接新数据
              this.setData({
                ptlist: this.data.ptlist.concat(arr)
              })
            }
          }
          /////////////////
        } else if (this.data.active == 1) {
          this.setData({
            zjtotal: zjtotal
          })
          //////////////////
          if (this.data.currentobj[1] == 1) {
            //如果是当前页我就给你赋值
            this.setData({
              zjlist: arr
            })
          } else {
            if (options == "缓存") {
              this.setData({
                zjlist: this.data.zjlist
              })
            } else {
              //如果不是当前页我就给你拼接新数据
              this.setData({
                zjlist: this.data.zjlist.concat(arr)
              })
            }
          }
          /////////////////
        } else if (this.data.active == 2) {
          this.setData({
            gjtotal: gjtotal
          })
          //////////////////
          if (this.data.currentobj[2] == 1) {
            //如果是当前页我就给你赋值
            this.setData({
              gjlist: arr
            })
          } else {
            if (options == "缓存") {
              this.setData({
                gjlist: this.data.gjlist
              })
            } else {
              //如果不是当前页我就给你拼接新数据
              this.setData({
                gjlist: this.data.gjlist.concat(arr)
              })
            }
          }
          /////////////////
        }
      })
      .catch(err => {
        wx.showToast({
          title: err,
        })
      })
  },
  //跳转至详情
  toDitail(e) {
    if (this.data.role){
      wx.showToast({
        title: '权限不足',
      })
      retrun
    }
    wx.navigateTo({
      url: '../finishStatus/finishStatus?orderid=' + e.currentTarget.dataset.orderid
    })
  },
  //上一页
  prevent() {
    let keys = "currentobj." + this.data.active
    this.setData({
      [keys]: 1
    })
    this.getList()
  },
  //下一页
  next() {
    let ptcurrent = this.data.currentobj[0]
    let zjcurrent = this.data.currentobj[1]
    let gjcurrent = this.data.currentobj[2]
    let keyone = 'currentobj.0'
    let keytow = 'currentobj.1'
    let three = 'currentobj.2'
    if (this.data.active === 0 && ptcurrent !== this.data.pttotal) {
      ptcurrent++
      this.setData({
        [keyone]: ptcurrent
      })
      this.getList()
    }


    if (this.data.active === 1 && zjcurrent !== this.data.zjtotal) {
      zjcurrent++
      this.setData({
        [keytow]: zjcurrent
      })
      this.getList()
    }

    if (this.data.active === 2 && gjcurrent !== this.data.gjtotal) {
      gjcurrent--
      this.setData({
        [keythree]: gjcurrent
      })
      this.getList()
    }
  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (wx.getStorageSync('userMsg').roleList == "role_admin" || wx.getStorageSync('userMsg').roleList == "role_superAdmin") {
      this.setData({
        role: false
      })
    }
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (getCurrentPages().length != 0) {
      //刷新当前页面的数据
      getCurrentPages()[getCurrentPages().length - 1].onLoad()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})